package com.pace.netty.echo.server;

import com.pace.netty.service.OrderService;
import com.pace.netty.service.OrderServiceImpl;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Package: com.pace.netty
 * Description： TODO
 * Author: Forest
 * Date: Created in 2021/4/5 16:41
 * Version: 0.0.1
 * Modified By: duforest
 */
public class EchoServerHandler extends ChannelInboundHandlerAdapter {

    private OrderService orderService;

    public EchoServerHandler() {
        if (orderService == null) {
            orderService = new OrderServiceImpl();
        }
    }

    //channle激活
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }

    //channel不可用
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
    }

    //收到数据
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        System.out.println("收到数据!msg={" + msg + "}");

        ByteBuf buf = (ByteBuf) msg;
        byte[] req = new byte[buf.readableBytes()];
        buf.readBytes(req);

        String request = new String(req, "utf-8");
        System.out.println("Client Request : " + request);
        request = orderService.transOrderDTO(request);

        ByteBuf resp = Unpooled.copiedBuffer(request.getBytes());
        ctx.writeAndFlush(resp);
    }

    //从流中读取数据结束
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    //事件触发
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        super.userEventTriggered(ctx, evt);
    }

    //异常处理
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//        super.exceptionCaught(ctx, cause);
        cause.printStackTrace();
        ctx.close();
    }
}
